package com.lv.suyiyong.hfrwork.ui

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ActivityUtils
import com.lv.common.base.BaseActivity
import com.lv.common.utils.*

import com.lv.service.repo.UserDbHelper
import com.lv.service.repo.UserInfo
import com.lv.service.webview.ConsealWebActivity
import com.lv.suyiyong.hfrwork.R
import com.lv.suyiyong.hfrwork.databinding.ActivitySettingBinding
import com.tencent.qcloud.tuicore.TUILogin
import com.tencent.qcloud.tuicore.interfaces.TUICallback

/**
 * 作者： ygx
 * 创建日期：2023/5/6 10:33 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：设置
 */
@Route(path = ARoutUtils.SettingActivity)
class SettingActivity : BaseActivity<ActivitySettingBinding>(),
    View.OnClickListener {
    @Autowired(name = "userinfo")
    @JvmField
    var mUserInfo: UserInfo? = null
    override fun getLayoutRes() = R.layout.activity_setting
    override fun initView() {
        super.initView()
        initStatusBar(this, true, true)
        ARouter.getInstance().inject(this)
        mBinding.apply {
            StatusBarUtil.setHeightAndPadding(this@SettingActivity, titleBar)
            titleBar.onClickBackIcon { finish() }
            titleBar.setTitle("设置")
            llConecl.setOnClickListener(this@SettingActivity)
            llConcealSetting.setOnClickListener(this@SettingActivity)
            llAboutSyy.setOnClickListener(this@SettingActivity)
            llFeedback.setOnClickListener(this@SettingActivity)
            llClearDask.setOnClickListener(this@SettingActivity)
            llClearUser.setOnClickListener(this@SettingActivity)
            tvLoginOut.setOnClickListener(this@SettingActivity)
            llConcealSample.setOnClickListener(this@SettingActivity)
            llUserinfoInventory.setOnClickListener(this@SettingActivity)
            llThirdShare.setOnClickListener(this@SettingActivity)
            llAuthority.setOnClickListener(this@SettingActivity)
            llCallphone.setOnClickListener(this@SettingActivity)
        }
    }


    override fun initConfig() {
        super.initConfig()
        UserDbHelper.getLiveUserInfo(this@SettingActivity).observeKt {
            mUserInfo = it
        }


    }


    override fun initData() {
        super.initData()
        val cacheSize = DataCleanManager.getTotalCacheSize(this)
        mBinding.tvDask.text = cacheSize
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {

            R.id.ll_conceal_setting -> {
                ARouter.getInstance().build(ARoutUtils.ConsealSettingActivity)
                    .withParcelable("userinfo", mUserInfo).navigation()
            }
            //隐私政策摘要 type 0 隐私政策 1 隐私政策摘要 2 与第三方共享个人信息清单 3 应用权限说明
            R.id.ll_conceal_sample -> {
                ConsealWebActivity.openUrl(this, "1")
            }
            R.id.ll_conecl -> {
                ConsealWebActivity.openUrl(this, "0")
            }
            R.id.ll_userinfo_inventory -> {
                ARouter.getInstance().build(ARoutUtils.VerifyCodeActivity)
                    .withParcelable("userinfo", mUserInfo).navigation()
            }
            R.id.ll_third_share -> {
                ConsealWebActivity.openUrl(this, "2")
            }
            R.id.ll_authority -> {
                ConsealWebActivity.openUrl(this, "3")
            }

            R.id.ll_about_syy -> {
                ARouter.getInstance().build(ARoutUtils.ABoutMineActivity).navigation()
            }
            R.id.ll_feedback -> {
                ARouter.getInstance().build(ARoutUtils.FeedBackActivity).navigation()
            }
            R.id.ll_callphone -> {
                YgxUtilsKt.callAllPhone(this, Constants.DMXS_TEL)
            }
            R.id.ll_clear_user -> {
                ARouter.getInstance().build(ARoutUtils.DeleteUserActivity)
                    .withParcelable("userinfo", mUserInfo)
                    .navigation()
            }
            R.id.ll_clear_dask -> {
                DataCleanManager.clearAllCache(this)
                showToast("清理完成")
                mBinding.tvDask.text = "0K"
                MyUtils.onClearMemory(this)
            }
            R.id.tv_login_out -> {
                UserDbHelper.deleteUserInfo(this@SettingActivity)
                //清除Token
                YgxUtilsKt.setAppToken("", "", "")
                TUILogin.logout(object : TUICallback() {
                    override fun onSuccess() {
                    }

                    override fun onError(errorCode: Int, errorMessage: String?) {
                    }
                })
                ActivityUtils.finishAllActivities()
                ARouter.getInstance().build(ARoutUtils.LoginActivityURL).navigation()

            }


        }
    }

}